/*
 *  Copyright 2010 UPF Telecos 4th group G.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package edu.upf.content;

import edu.upf.utils.LogSystem;
import org.xml.sax.*;

/**
 * Class that allows to handle an xml string and parses it to an ImageObject
 * @author netjois
 */
public class ImageHandler extends org.xml.sax.helpers.DefaultHandler {

    private ImageObject photo;
    private StringBuffer accumulator;
    private LogSystem log;


    /**
     * Basic constructor, initiates a new ImageObject instance
     */
    public ImageHandler() {
	photo = new ImageObject();
        log = new LogSystem(VideoHandler.class);
    }

    /**
     * Specific constructor, receives the image object to fill data in
     * @param ph
     */
    public ImageHandler(ImageObject ph) {
	photo = ph;
        log = new LogSystem(VideoHandler.class);
    }

    @Override
    public void startDocument() {
	accumulator = new StringBuffer();
    }

    // When the parser encounters plain text (not XML elements), it calls this method, which accumulates
    // the text in a string buffer. Note that this method may be called in multiple times.
    @Override
    public void characters(char[] buffer, int start, int length) {
	accumulator.append(buffer, start, length);
    }

    // At the beginning of each new element, erase any accumulated text
    @Override
    public void startElement(String namespaceURL, String localName, String qname, Attributes attributes) {
	accumulator.setLength(0);
    }

    // Take special action when reach the end of selected elements.
    // In this case the accumulated text is stored in the photo object
    @Override
    public void endElement(String namespaceURL, String localName, String qname)
    {
	if (localName.equals("name")) {
		photo.setName(accumulator.toString().trim());
	}
        else if (localName.equals("size")) {
		photo.setSize(accumulator.toString().trim());
	}
        else if (localName.equals("path")) {
		photo.setAlbum(accumulator.toString().trim());
	}
        else if (localName.equals("popularity")) {
		photo.setPopularity(accumulator.toString().trim());
	}
        else if (localName.equals("format")) {
		photo.setFormat(accumulator.toString().trim());
	}
    }

    // Called at the end
    @Override
    public void endDocument() {
    }

    @Override
    public void warning(SAXParseException exception) {
	log.addInfoLog("WARNING: line " + exception.getLineNumber() + ": "+
			   exception.getMessage());
    }

    @Override
    public void error(SAXParseException exception) {
	log.addInfoLog("ERROR: line " + exception.getLineNumber() + ": " +
			   exception.getMessage());
    }

    @Override
    public void fatalError(SAXParseException exception) throws SAXException {
	log.addInfoLog("FATAL: line " + exception.getLineNumber() + ": " +
			   exception.getMessage());
	throw(exception);
    }
}

